/*
* 相机;
*/
class Camera2D{
    /**视窗范围 */
    private _cameraView:Laya.Rectangle;

    private _scene:Sprite;
    /**跟随目标 */
    private _focusTarget:Sprite;
    constructor(scene:Sprite){
        this._scene = scene;
        this._cameraView = new Laya.Rectangle(0,0,GameConfig.deviceW,GameConfig.deviceH);
        this._scene.scrollRect = this._cameraView;
    }

    /**视窗范围 */
    public get cameraView() : Laya.Rectangle {
        return this._cameraView;
    }

    /**
     * 跟随
     * @param target 跟随目标
     */
    public focus(target:Sprite):void
    {
        this._focusTarget = target;
        this._cameraView.x =  this._focusTarget.x - (GameConfig.deviceW >> 1);
        this._cameraView.y =  this._focusTarget.y - (GameConfig.deviceH >> 1)
    }

    private _pos:Laya.Point = new Laya.Point();

    private _ease:number = 0.025;
    /**
     * 更新
     */
    public update():void
    {
        if(this._focusTarget != null)
        {   
            this._pos.x = this._focusTarget.x - (GameConfig.deviceW >> 1);
            this._pos.y = this._focusTarget.y - (GameConfig.deviceH >> 1)
            let mapWidth:number =  WorldMap.instance.gridVO.gridW;
            let mapHeigh:number = WorldMap.instance.gridVO.gridH;
            if((this._focusTarget.x + (GameConfig.deviceW >> 1)) < mapWidth
                && (this._focusTarget.x - (GameConfig.deviceW >> 1))> 0)
            {
                this._cameraView.x += (this._pos.x - this._cameraView.x)*this._ease*Laya.timer.delta;
            }
            if((this._focusTarget.y + (GameConfig.deviceH >> 1)) < mapHeigh
                && (this._focusTarget.y - (GameConfig.deviceH >> 1))> 0)
            {
                this._cameraView.y += (this._pos.y - this._cameraView.y)*this._ease*Laya.timer.delta;
            }
        }
    }
}